/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkgjeudestrategie;

import java.awt.Color;
import java.awt.Graphics;


import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import pkgapplication.UniteDeCombat;
import pkgjeudestrategie.Batiment.NiveauBatiment;
import pkgjeudestrategie.Batiment.TypeBatiment;
import pkgjeudestrategie.ressource.Ressource;

/**
 *
 * @author Admin
 */
public class EcranInfos extends JPanel {

    private Fenetre fenetre;
    Terrain t = null;
    int ressourceLocation = -1;
    int terrainType = -1;
    int constructionOption = 0;
    int emplacementActuel = 0;
    int actualTabLocation = 0;
    int batimentType = 0;
    int bombardement=-1;
    boolean bombardementB=false;
    boolean isBatiment = false;
    boolean moving = false;
    Terrain oldTerrain;
    Territoire colonisedTerritoire = null;
    Drapeau drapeau = null;
    ArrayList<Faction> factionList = new ArrayList();
    ArrayList<Player> playerList = new ArrayList();
    TimeButton timeButton;
    FicheButton ficheButton = new FicheButton();
    JLabel lblFaction = new JLabel(" ");
    JLabel lblName = new JLabel(" ");
    JButton btnDiplomatie = new JButton("Action diplomatique");
    JButton btnConstruction = new JButton("Construction");
    JButton btnRessource = new JButton("Ressources");
    JButton btnColonize = new JButton("Coloniser");
    JButton btnDeplacementMilitaire = new JButton("Déplacement Militaire");
    JButton btnWar = new JButton("Déclarer la guerre");
    JButton btnTraite = new JButton("Demander un traité commercial");
    JButton btnBombarder = new JButton("Bombarder");
    JButton btnChimique = new JButton("Bombardement chimique");
    JButton btnAtomique = new JButton("Bombardement atomique");
    JButton btnConstruire = new JButton("Construire");
    JButton btnDetruire = new JButton("Détruire");
    JButton btnPrivatiser = new JButton("Privatiser");
    JButton btnNationaliser = new JButton("Nationailser");
    JButton btnEmplacement1 = new JButton("Emplacement vide");
    JButton btnEmplacement2 = new JButton("Emplacement vide");
    JButton btnEmplacement3 = new JButton("Emplacement vide");
    JButton btnEmplacement4 = new JButton("Emplacement vide");
    JButton btnRessource1 = new JButton(" ");
    JButton btnRessource2 = new JButton(" ");
    JButton btnRessource3 = new JButton(" ");
    JButton btnRessource4 = new JButton(" ");
    JButton btnCaserne = new JButton("Caserne");
    JButton btnHotelVille = new JButton("Hotel de ville");
    JButton btnMarche = new JButton("Marche");
    JButton btnCampRecrutement = new JButton("Camp de recrutement");
    JButton btnAeroport = new JButton("Aeroport");
    JButton btnUniversite = new JButton("Université");
    JButton btnTheatre = new JButton("Théatre");
    JButton btnLaboratoire = new JButton("Laboratoire");
    JButton btnHopital = new JButton("Hopital");
    JButton btnVide = new JButton("Retour");
    JButton btnFaireLeBatiment = new JButton("Construire");
    JButton btnRecrutement = new JButton("Recrutement");
    JButton btnValidateRecrutement = new JButton("Recruter");
    JButton[] tabIni = {btnDiplomatie, btnConstruction, btnRessource, btnColonize, btnRecrutement, btnDeplacementMilitaire};
    JButton[] tabDiplomatie = {btnWar, btnTraite,btnBombarder, btnChimique, btnAtomique, btnVide};
    JButton[] tabConstructionOption = {btnConstruire, btnDetruire, btnPrivatiser, btnVide};
    JButton[] tabConstruction = {btnCaserne, btnHotelVille, btnMarche, btnCampRecrutement, btnAeroport, btnUniversite, btnTheatre, btnLaboratoire, btnHopital, btnVide};
    JButton[] tabRessourceEmplacement = {btnRessource1, btnRessource2, btnRessource3, btnRessource4, btnVide};
    JButton btnDeplacer = new JButton("Déplacer");
    JButton[] tabRessource = {btnConstruire, btnVide};
    JSlider sldNbrUniteLegere = new JSlider();
    JSlider sldNbrUniteLourde = new JSlider();
    JSlider sldNbrUniteAerienne = new JSlider();
    JLabel lblNbrUniteLegere = new JLabel("Nombre d'unités légères : 0");
    JLabel lblNbrUniteLourde = new JLabel("Nombre d'unités lourdes : 0");
    JLabel lblNbrUniteAerienne = new JLabel("Nombre d'unités aériennes : 0");
    JComponent[] tabRecrutement = {lblNbrUniteLegere, sldNbrUniteLegere, lblNbrUniteLourde, sldNbrUniteLourde, lblNbrUniteAerienne, sldNbrUniteAerienne, btnValidateRecrutement, btnVide};
    JComboBox choixDeVille = new JComboBox();
    JComponent[] tabDeplacement = {lblNbrUniteLegere, sldNbrUniteLegere, lblNbrUniteLourde, sldNbrUniteLourde, lblNbrUniteAerienne, sldNbrUniteAerienne, choixDeVille, btnDeplacer, btnVide};
    //ce n'est pas automatiquement le joueur si isNotPlayer=fasle

    public EcranInfos(Fenetre fenetre) {

        super(new GridLayout(14, 0));
        this.fenetre = fenetre;
        listen();
        setFocusable(true);
        setSize(200, Fenetre.tailleY);
        setEnabled(true);
        addMouseListener(new MouseAdapter() {

            @Override
            public void mouseEntered(MouseEvent e) {
                moving = true;
            }

            @Override
            public void mouseExited(MouseEvent e) {
                moving = false;
            }
        });

    }

    public void listeDeVille() {
        this.choixDeVille.removeAllItems();
        for (String temp : this.getActualFaction().nomTerritoire()) {
            this.choixDeVille.addItem(temp);

        }
        for(Faction temp : this.fenetre.getJeu().getMonde().getFactionList()){
        if(this.getActualFaction().getDiplomatie(temp).equals("En guerre")){
        for (String temp2 : temp.nomTerritoire()) {
            this.choixDeVille.addItem(temp2);
}
        
        
        }
        }
        
    }

    private void deplacerLesUnite(Territoire territoire) {
        ArrayList<UniteDeCombat>ArmyRenfort=new ArrayList();
         int quantitedeTour= fenetre.getJeu().getMonde().meilleurTrajectoireDeDéplacement(territoire,this.getActualFaction().findTerritoire((String)this.choixDeVille.getSelectedItem()));
        territoire.perteDunite(sldNbrUniteLegere.getValue(), 0,quantitedeTour,this.getActualFaction().findTerritoire((String)this.choixDeVille.getSelectedItem()));
        territoire.perteDunite(sldNbrUniteLourde.getValue(), 1,quantitedeTour,this.getActualFaction().findTerritoire((String)this.choixDeVille.getSelectedItem()));
          territoire.perteDunite(sldNbrUniteAerienne.getValue(), 2,quantitedeTour,this.getActualFaction().findTerritoire((String)this.choixDeVille.getSelectedItem()));
  
    }
    



    public int isMoving() {
        try {
            if (moving) {
                if (getMousePosition().getX() == 0 && getMousePosition().getY() == 0) {
                    return 0;
                } else if (getMousePosition().getX() == 0 && getMousePosition().getY() > (Fenetre.tailleY - 10)) {
                    return 1;
                } else if (getMousePosition().getY() == 0) {
                    return 2;
                } else if (getMousePosition().getX() == 0) {
                    return 3;
                } else if (getMousePosition().getY() > (Fenetre.tailleY - 10)) {
                    return 4;
                }
            }
        } catch (Exception e) {
        }

        return -1;
    }

    public void slideInformation(boolean deplacement) {
        Territoire temporaire = (Territoire) this.fenetre.getJeu().getMonde().getClickedTerrainState();
    int[] tabDeplacement={temporaire.nbUniteTerrestreLegere(),temporaire.nbUniteTerrestreLourde(),temporaire.nbUniteAerienne()};
    JSlider[] tabSlider={sldNbrUniteLegere,sldNbrUniteLourde,sldNbrUniteAerienne};
    int max=1000; //va changer avec la caserne

    if(deplacement){for(int i=0;i<3;i++){
       if(i==0){tabSlider[i].setMaximum(tabDeplacement[i] - 600);//parce qu'il faut avoir un minimum de 600 unite legere par territoire
       
        tabSlider[i].setMinorTickSpacing((tabDeplacement[i]-600)/2);
        tabSlider[i].setMajorTickSpacing((tabDeplacement[i]-600));}
       else {tabSlider[i].setMaximum(tabDeplacement[i]);
        tabSlider[i].setMinorTickSpacing(tabDeplacement[i]/4);
        tabSlider[i].setMajorTickSpacing(tabDeplacement[i]);}
       tabSlider[i].setMinimum(0);
        tabSlider[i].setValue(0);
        tabSlider[i].setPaintTicks(true);
        tabSlider[i].setPaintLabels(true);

}}
    else{
        for(int i=0;i<3;i++){
           tabSlider[i].setMaximum(max); 
        tabSlider[i].setMinorTickSpacing(max/4);
        tabSlider[i].setMajorTickSpacing(max);            
        }
        
    }


        sldNbrUniteLegere.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent event) {
                lblNbrUniteLegere.setText("Nombre d'unités légères : "
                        + ((JSlider) event.getSource()).getValue());
            }
        });

        sldNbrUniteLourde.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent event) {
                lblNbrUniteLourde.setText("Nombre d'unités lourdes : "
                        + ((JSlider) event.getSource()).getValue());
            }
        });
        sldNbrUniteAerienne.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent event) {
                lblNbrUniteAerienne.setText("Nombre d'unités aériennes : "
                        + ((JSlider) event.getSource()).getValue());
            }
        });
    }

    public void fillThis(JComponent[] pnl) {
        removeAll();
        add(lblFaction,JLabel.CENTER);
        if (drapeau != null) {
            add(drapeau);
        } else {
            add(lblName,JLabel.CENTER);
        }
        for (int i = 0; i < pnl.length; i++) {
            add(pnl[i]);
        }
        for (int x = pnl.length - 1; x < 9; x++) {
            add(new JLabel(" "));
        }
        JPanel pnl2 = new JPanel(new GridLayout(0, 2));
        pnl2.add(ficheButton);
        pnl2.add(timeButton);
        add(pnl2);
        repaint();

    }

    public void fillThisList(ArrayList<JButton> liste) {
        JComponent[] pnl = new JComponent[liste.size()];
        int x = 0;
        for (JButton btn : liste) {
            pnl[x] = btn;
            x++;
        }

        fillThis(pnl);

    }

    public void undo() {
        //      0          0                       1                         2                   3                  4
        /**   tabIni
         *          -> btnDiplomatie           =>tabWhatever
         *          -> btnConstructionOption   => tabEmplacement => tabConstructionOption => tabConstruction => tabWhatever
         *          -> btnRessourcesEmplacement=>tabWhatever
         *          -> btnrecrutement
         *          -> btnDeplacement
         *
         */
        if (actualTabLocation == 0) {
            fillThis(tabIni);
            actualTabLocation = 0;
        } else if (actualTabLocation == 1) {
            fillThis(tabIni);
            actualTabLocation = 0;
        } else if (actualTabLocation == 2) {
            constructionState();
            actualTabLocation = 1;
        } else if (actualTabLocation == 3) {
            fillThis(tabConstructionOption);
            actualTabLocation = 2;
        } else if (actualTabLocation == 4) {
            fillThis(tabConstruction);
            actualTabLocation = 3;
        }else if (actualTabLocation == 5) {
            fillThis(tabDiplomatie);
            actualTabLocation = 0;
        }

        repaint();
    }

    public void constructionState() {
        // à utiliser uniquement avec des Territoires
        if (t instanceof Territoire) {
            ArrayList<JButton> listButton = new ArrayList();
            JButton[] tab = {btnEmplacement1, btnEmplacement2, btnEmplacement3, btnEmplacement4};

            Batiment[] tabBatiment = t.getBatiment();
            int y = 0;
            //Crée la liste des batiments construits, plus les emplacements vides
            for (int x = 0; x < tabBatiment.length; x++) {
                listButton.add(tab[x]);
                listButton.get(listButton.size() - 1).setText(tabBatiment[x].toString());
                if (!listButton.get(listButton.size() - 1).getText().equalsIgnoreCase("Emplacement vide") && tabBatiment[x].isActivated()) {
                    listButton.get(listButton.size() - 1).setText(listButton.get(listButton.size() - 1).getText() + " ...");
                }


            }
            if (constructionOption == 0) {
                listButton.add(btnVide);
                fillThisList(listButton);
            } else if (constructionOption == 1) {
                fillThisList(listButton);
            }
        }

    }

    public boolean compare(JButton btn, Batiment b) {
        if (btn.getText().regionMatches(0, b.toString(), 0, 5)) {
            return false;

        }

        return true;
    }

    public void setFactionList(ArrayList<Faction> list) {
        factionList = list;
    }

    public void setPlayerList(ArrayList<Player> list) {

        playerList = list;
        timeButton = new TimeButton(playerList.size());
    }

    public Faction getActualFaction() {
        Faction f = playerList.get(timeButton.getPlayerTurn()).getFaction();
        return f;
    }

    public String getRigthFaction(int i) {
        String s = "Gaia";
        for (Faction f : factionList) {
            if (f.contains(t)) {
                Territoire te = (Territoire) t;
                drapeau = f.getDrapeau(te);
                s = f.getName();
                if (f.isTraite(getActualFaction()) || f.atWar(getActualFaction())) {
                    btnTraite.setEnabled(false);
                } else {
                    btnTraite.setEnabled(true);
                }
                if (i == 0 && terrainType == 2 && getActualFaction() != f) {
                    String s2 = (f.getDiplomatie(getActualFaction()));
                    if (s2.equalsIgnoreCase("En guerre")) {
                        btnWar.setText("Demander la paix");
                        btnBombarder.setEnabled(false);
                        btnChimique.setEnabled(false);
                        btnAtomique.setEnabled(false);
                    } else {

                        btnWar.setText("Déclarer la guerre");
                        btnBombarder.setEnabled(true);
                        btnChimique.setEnabled(true);
                        btnAtomique.setEnabled(true);
                    }
                    s = s + "  (" + s2 + ")";
                } else if (i == 1) {
                    if (f.getDiplomatie(getActualFaction()).equalsIgnoreCase("En guerre")) {
                        if (f.askForPeace(getActualFaction())) {
                            JOptionPane.showMessageDialog(null, "La paix est acceptée");
                        } else {
                            JOptionPane.showMessageDialog(null, "La paix est refusée");
                        }
                    } else {
                        f.atWarWith(getActualFaction());
                        String s3 = "";
                        if (!btnTraite.isEnabled()) {
                            s3 = "Votre traité commercial avec cette faction n'est plus en vigueur.";
                            f.removeTraite(getActualFaction());
                        }
                        JOptionPane.showMessageDialog(null, "C'est la GUERRE!!!\n" + s3);
                    }
                } else if (i == 2) {
                    if (btnTraite.isEnabled()) {
                        f.getTraite(getActualFaction());

                        JOptionPane.showMessageDialog(null, "Sweet money!!!");
                    }
                }
            }
        }
        return s;
    }
public boolean isNewRealTurn(){
    boolean b=timeButton.getNewRealTurn();
    timeButton.setNewRealTurn(false);
    return b;
}
    public boolean playerSwitch() {
        if (timeButton.getNewPlayer()) {
            timeButton.setNewPlayerFalse();
            return true;

        }
        return false;
    }

    public int getPlayerTurn() {
        return timeButton.getPlayerTurn();
    }

    public void setStateClickedTerrain(Terrain t) {

        if (t != null) {

            this.t = t;
            terrainType = t.getState(timeButton.getPlayerTurn()); //0=capitale, 1=ville joueur, 2=ville ia, 3=terrain infranchissable, 4=terrain colonisable


            if (getActualFaction().contains(t)) {
                if(t.isLiving()){setBtnTabIniEnabled(false, true, true, false, true, true);}
                else{setBtnTabIniEnabled(false, false, false,false, false, false);}
            } else if (terrainType == 2) {
                setBtnTabIniEnabled(true, false, false, false, false, false);
            } else if (terrainType == 3) {
                drapeau = null;
                setBtnTabIniEnabled(false, false, false, false, false, false);
            } else if (terrainType == 4 || terrainType == 5) {
                setBtnTabIniEnabled(false, false, false, true, false, false);
                drapeau = null;
            }

            String factionName = getRigthFaction(0);
            lblFaction.setText(factionName);
            lblName.setText(t.getName());


            if (oldTerrain != null) {
                if (oldTerrain != t) {
                    fillThis(tabIni);
                    oldTerrain = t;
                }


            } else {
                oldTerrain = t;
                fillThis(tabIni);
            }
        }

    }

    public void setBtnTabIniEnabled(boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6) {

        btnDiplomatie.setEnabled(b1);
        btnConstruction.setEnabled(b2);
        btnRessource.setEnabled(b3);
        btnColonize.setEnabled(b4);
        btnRecrutement.setEnabled(b5);
        btnDeplacementMilitaire.setEnabled(b6);
    }

    public void constructionOptions(int i) {
        tabConstructionOption[0].setEnabled(true);
        tabConstructionOption[1].setEnabled(true);
        tabConstructionOption[2].setEnabled(true);
        Batiment[] tabBatiment = t.getBatiment();
        if (tabBatiment[i].getNiveau() == NiveauBatiment.NIV_5 && tabBatiment[i].getRawType() != TypeBatiment.VIDE) {
            tabConstructionOption[0].setEnabled(false);
        } else if (tabBatiment[i].getRawType() != TypeBatiment.VIDE) {
            tabConstructionOption[0].setText("Améliorer");
            if (tabBatiment[i].isActivated()) {
                tabConstructionOption[0].setEnabled(false);

            }

        } else if (tabBatiment[i].getRawType() == TypeBatiment.VIDE) {
            tabConstructionOption[0].setText("Construire");
            tabConstructionOption[1].setEnabled(false);
            tabConstructionOption[2].setEnabled(false);

        }
        if (tabBatiment[i].isPrivatised()) {
            tabConstructionOption[2].setText("Nationaliser");

        } else {
            tabConstructionOption[2].setText("Privatiser");

        }
        emplacementActuel = i;
        fillThis(tabConstructionOption);


    }

    public void inTheOption() {
        boolean atLeastOneMatch = false;
        Batiment[] tabBat = t.getBatiment();
        ArrayList<JButton> listBat = new ArrayList();
        ArrayList<JButton> listDest = new ArrayList();
        for (int u = 0; u < tabConstruction.length; u++) {

            for (Batiment b : tabBat) {
                if (!compare(tabConstruction[u], b)) {
                    atLeastOneMatch = true;
                }
            }
            if (atLeastOneMatch) {
                listDest.add(tabConstruction[u]);
                atLeastOneMatch = false;
            } else {
                listBat.add(tabConstruction[u]);

            }
        }
        if (constructionOption == 0 && t.noSuchBuilding(getType(emplacementActuel))) {
            fillThisList(listBat);


        } else if (constructionOption == 0) {
            undo();
        } else if (constructionOption == 1) {
            use(emplacementActuel);

        } else if (constructionOption == 2) {
            //rien pour l'instant
        }
    }

    public boolean isNotUpgrade(int i) {
        JButton[] tab = {btnEmplacement1, btnEmplacement2, btnEmplacement3, btnEmplacement4};
        if (tab[i].getText().regionMatches(0, "Emplacement", 0, 5)) {
            return true;
        }
        return false;
    }

    public void use(int i) {
        if (constructionOption == 0) {
            t.construire(i);
            playerList.get(timeButton.getPlayerTurn()).achat(t.getDepenseBatiment(i));
        } else if (constructionOption == 1) {
            t.detruire(i);
        } else if (constructionOption == 2) {
            t.privatiser(i);
        }

        undo();

    }

    public void setRessources() {
        JButton[] tab = {btnRessource1, btnRessource2, btnRessource3, btnRessource4};
        if (t instanceof Territoire) {
            Territoire te = (Territoire) t;
            int x = 0;
            for (Ressource r : te.getRessources()) {
                tab[x].setText(r.toString());
                if (!r.isUpgradable()) {
                    tab[x].setEnabled(false);
                }
                if (r.isActivated()) {
                    tab[x].setText(tab[x].getText() + "...");
                }
                x++;
            }
        }
    }
    public Terrain getTerrainClicked(){
        return t;
    }
    public void actualizeScreen(){
        
        
        
        lblName.setText(t.getName());
        fillThis(tabIni);
    }
public void resetBombardement(){
    bombardementB=false;
    actualizeScreen();
}
public int getBombardement(){
    if(!bombardementB){
        return -1;
    }
    return bombardement;
        }
    public int getType(int emplacement) {
        int i = -1;
        JButton[] tab = {btnEmplacement1, btnEmplacement2, btnEmplacement3, btnEmplacement4};
        String s = tab[emplacement].getText();

        if (s.regionMatches(0, "Caserne", 0, 5)) {
            i = 0;
        } else if (s.regionMatches(0, "Hotel de ville", 0, 5)) {
            i = 1;
        } else if (s.regionMatches(0, "Marché", 0, 5)) {
            i = 2;
        } else if (s.regionMatches(0, "Camp de recrutement", 0, 5)) {
            i = 3;
        } else if (s.regionMatches(0, "Aéroport", 0, 5)) {
            i = 4;
        } else if (s.regionMatches(0, "Université", 0, 5)) {
            i = 5;
        } else if (s.regionMatches(0, "Théatre", 0, 5)) {
            i = 6;
        } else if (s.regionMatches(0, "Laboratoire", 0, 5)) {
            i = 7;
        } else if (s.regionMatches(0, "Hopital", 0, 5)) {
            i = 8;
        }
        return i;
    }
//*Cost c'est seulement pour bombardement (qui n'a pas de classe)
    public void getInfoPanel(int y, int i,int cost) {
        //d: img, lblNom,txtDescription,txtActualEffects
        Description d = new Description(y, i);
        if (y == 1) {
            isBatiment = true;
        }
        removeAll();
        add(lblFaction);
        if (drapeau != null) {
            add(drapeau);
        } else {
            add(lblName);
        }
        JComponent[] tab = d.getComponent();
        JPanel pnl1 = new JPanel(new GridLayout(0, 2));
        if (y != 3) {
            pnl1.add(tab[0]);
        }
        pnl1.add(tab[1]);
        add(pnl1);
        tab[2].setEnabled(false);
        tab[2].setBackground(Color.red);
        add(tab[2]);

        add(btnFaireLeBatiment);
        if (playerList.get(timeButton.getPlayerTurn()).getsStatsJoueur().getSommeTotale() < Batiment.getMoneyStats(i, 0) && y == 1) {
            btnFaireLeBatiment.setEnabled(false);
        } else if (playerList.get(timeButton.getPlayerTurn()).getsStatsJoueur().getSommeTotale() < t.getRessources()[i].getCout(true) && y == 2) {
            btnFaireLeBatiment.setEnabled(false);
        } else if(playerList.get(timeButton.getPlayerTurn()).getsStatsJoueur().getSommeTotale()<cost){
        btnFaireLeBatiment.setEnabled(false);
        
        
        }
        else {
            btnFaireLeBatiment.setEnabled(true);
            if(y>=0&&y<=3){
                btnFaireLeBatiment.setText("Faire la construction"); 
            }
            else if(y==4){
               btnFaireLeBatiment.setText("Bombarder"); 
            }
        }
        add(btnFaireLeBatiment);


        add(btnVide);
        for (int x = 0; x < 6; x++) {
            add(new JLabel(" "));
        }
        JPanel pnl2 = new JPanel(new GridLayout(0, 2));
        pnl2.add(ficheButton);
        pnl2.add(timeButton);
        add(pnl2);
        repaint();

    }

    public boolean isColonizable(Territoire te) {
        try {
        } catch (Exception e) {
        }


        return true;
    }

    public Territoire getColonisedTerritoire() {
        return colonisedTerritoire;
    }

    public void setNullColonisedTerritoire() {
        for (Faction f : factionList) {
            if (f.contains(t)) {
                Territoire te = (Territoire) t;
                drapeau = f.getDrapeau(te);
            }
        }
        undo();
        colonisedTerritoire = null;
    }

    public void getFicheButtonActivated() {
        if (ficheButton.activatedFiche) {
            ficheButton.setActivatedFiche(false);
            //System.out.println(getActualFaction());
            new Fiche(getActualFaction());
        }

    }

    public void setTableauButtonConstruction(int i) {
        getInfoPanel(1, i,0);
        batimentType = i;
        actualTabLocation = 4;
    }

    public void validateRecruiting() {
        int x = sldNbrUniteLegere.getValue();
        int y = sldNbrUniteLourde.getValue();
        int z = sldNbrUniteAerienne.getValue();
        if (t instanceof Territoire) {
            Territoire te = (Territoire) t;
            te.addTroops(x, y, z);
        }
    }
public void getIntoDiplomatie(){
    
                fillThis(tabDiplomatie);
}
    public void listen() {
        btnDiplomatie.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                getIntoDiplomatie();


            }
        });
        btnBombarder.addActionListener(new ActionListener() {
//normal=0, chimique=1, atomique=2
            public void actionPerformed(ActionEvent ae) {

            actualTabLocation = 5;
                bombardement=0;
                getInfoPanel(4, bombardement,750);
            }
        }); 
        btnChimique.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {

            actualTabLocation = 5;
                bombardement=1;
                getInfoPanel(4, bombardement,3000);


            }
        }); 
        btnAtomique.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {

            actualTabLocation = 5;
                bombardement=2;
                getInfoPanel(4, bombardement,5000);


            }
        });
        btnConstruction.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                constructionState();
                //fillThis(tabConstructionOption);
                actualTabLocation = 0;
            }
        });
        btnRessource.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                setRessources();
                fillThis(tabRessourceEmplacement);
                actualTabLocation = 0;
            }
        });
        btnColonize.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                if (t instanceof Territoire) {
                    Territoire te = (Territoire) t;
                    colonisedTerritoire = te;
                    //if(isColonizable(te)){
                    //te.setOccupied();
                    //playerList.get(0).getFaction().addTerritoire(te);
                    //  JOptionPane.showMessageDialog(null, "Vous avex un nouveau territoire: "+te.getName());  }
                }
            }
        });
        btnConstruire.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                if (btnConstruire.getText().equalsIgnoreCase("Construire")) {
                    actualTabLocation = 3;
                } else {
                    actualTabLocation = 0;
                }
                constructionOption = 0;
                inTheOption();
                //constructionState();
            }
        });
        btnDetruire.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                constructionOption = 1;
                inTheOption();
                //constructionState();
            }
        });
        btnPrivatiser.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                constructionOption = 2;
                inTheOption();
                //constructionState();
            }
        });



        btnWar.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                getRigthFaction(1);
                undo();
            }
        });
        btnTraite.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                getRigthFaction(2);
                undo();
            }
        });

        btnCaserne.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                setTableauButtonConstruction(0);
            }
        });
        btnHotelVille.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                setTableauButtonConstruction(1);
            }
        });
        btnMarche.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                setTableauButtonConstruction(2);
            }
        });
        btnCampRecrutement.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                setTableauButtonConstruction(3);
            }
        });
        btnAeroport.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                setTableauButtonConstruction(4);
            }
        });
        btnUniversite.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                setTableauButtonConstruction(5);
            }
        });
        btnTheatre.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                setTableauButtonConstruction(6);
            }
        });
        btnLaboratoire.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                setTableauButtonConstruction(7);
            }
        });
        btnHopital.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                setTableauButtonConstruction(8);
            }
        });
        btnVide.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                undo();
            }
        });
        btnEmplacement1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                actualTabLocation = 2;
                constructionOptions(0);
                // use(getType(btnEmplacement1));
            }
        });
        btnEmplacement2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                actualTabLocation = 2;
                constructionOptions(1);
                // use(getType(btnEmplacement2));
            }
        });
        btnEmplacement3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                actualTabLocation = 2;
                constructionOptions(2);
                //use(getType(btnEmplacement3));
            }
        });
        btnEmplacement4.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                actualTabLocation = 2;
                constructionOptions(3);
                // use(getType(btnEmplacement4));
            }
        });
        btnFaireLeBatiment.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                actualTabLocation = 0;
                if (isBatiment) {
                    use(batimentType);
                    isBatiment = false;
                }
                else if(bombardement!=-1){
                bombardementB=true;
                bombardement=-1;
                }
                else {
                    t.upgradeRessource(ressourceLocation);
                    playerList.get(timeButton.getPlayerTurn()).achat(t.getDepenseRessource(ressourceLocation));
                    ressourceLocation = -1;
                    actualTabLocation = 0;
                    undo();
                }
            }
        });
        btnRessource1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                

                ressourceLocation = 0;
                getInfoPanel(3, t.getRessourceToInt(ressourceLocation),0);


            }
        });
        btnRessource2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
               
                ressourceLocation = 1;
                getInfoPanel(3, t.getRessourceToInt(ressourceLocation),0);

            }
        });
        btnRessource3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {

                ressourceLocation = 2;
                getInfoPanel(3, t.getRessourceToInt(ressourceLocation),0);

            }
        });
        btnRessource4.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {

                ressourceLocation = 3;
                getInfoPanel(3, t.getRessourceToInt(ressourceLocation),0);

            }
        });
        btnDeplacementMilitaire.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                slideInformation(true);
                listeDeVille();
                actualTabLocation = 0;
                fillThis(tabDeplacement);
                repaint();
            }
        });
        btnRecrutement.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {

                slideInformation(false);
                actualTabLocation = 0;
                fillThis(tabRecrutement);
            }
        });
        btnValidateRecrutement.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                validateRecruiting();
                actualTabLocation = 0;
                undo();
            }
        });
        btnDeplacer.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {

                
               deplacerLesUnite((Territoire)fenetre.getJeu().getMonde().getClickedTerrainState());
               fillThis(tabIni);


            }
        });
    }

    public void paintComponent(Graphics g) {
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(0, 0, 200, Fenetre.tailleY);
    }
}
